package cn.hxzy.controller;

import cn.hxzy.domain.*;
import cn.hxzy.response.R;
import cn.hxzy.response.StatusEnum;
import cn.hxzy.service.CourseService;
import cn.hxzy.service.DirectionService;
import cn.hxzy.service.ProfessionalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

// 假设CourseService是处理业务逻辑的服务类
@RestController
@RequestMapping("/courses")
public class CourseController {

    @Autowired
    private CourseService courseService;
    @Autowired
    private DirectionService directionService;
    @Autowired
    private ProfessionalService professionalService;



    // 创建课程
    @PostMapping("/createCourse")
    public R createCourse(@RequestBody Course course) {
        return courseService.createCourse(course);
    }

    /**
     * 给班级赋权
     *
     * @param courseId 课程id
     * @param classId  班级id
     * @return 影响行数
     */
    @PostMapping("/problems/grantClass")
    public R grantClass(@RequestParam("courseId") Long courseId, @RequestParam("classId") List<Integer> classIdList) {
        if (classIdList.size() == 0) {
            return R.error(StatusEnum.SERVICE_ERROR);
        }
        int value = courseService.addGrantClass(courseId, classIdList);
        if (value > 0) {
            return R.success("赋权成功");
        }
        return R.error(StatusEnum.SERVICE_ERROR);
    }

    /**
     * 根据班级ID获取课程
     * @param classId 班级编号
     * @return
     */
    @GetMapping("/classCourse")
    public R classCourse(@RequestParam("classId") Long classId) {
        List<Integer> courseIdByClassId = courseService.getCourseIdByClassId(classId);
        return R.success(courseIdByClassId);
    }

    // 修改课程
    @PutMapping("/updateCourse")
    public R updateCourse(@RequestBody Course courseDetails) {
        return courseService.updateCourse(courseDetails);
    }

    // 删除课程
    @DeleteMapping("/{id}")
    public R deleteCourse(@PathVariable Long id) {
        Boolean bool = courseService.deleteCourse(id);
        if (bool) {
            return R.success(id);
        } else {
            return R.error(400, "删除失败");
        }
    }

    //章节列表
    @GetMapping("/listChapter")
    public R<List<Chapter>> listChapter(Long courseId) {
        return courseService.listChapter(courseId);
    }

    // 创建章节
    @PostMapping("/chapters")
    public R createChapter(@RequestBody Chapter chapter) {
        R chapter1 = courseService.createChapter(chapter);
        if (chapter1 != null) {
            return R.success(chapter1);
        } else {
            return R.error(StatusEnum.SERVICE_ERROR);
        }

    }

    /**
     * 根据章节编号查询小节列表
     *
     * @param chapterId
     * @return
     */
    @GetMapping("/listSection/{chapterId}")
    public R<List<Section>> listSection(@PathVariable Long chapterId) {
        List<Section> sections = courseService.listSection(chapterId);
        return R.success(sections);

    }

    // 创建小节
    @PostMapping("/section")
    public R createSection(@RequestBody Section section) {
        return courseService.createSection(section);

    }

    /**
     * 根据课程名称搜索课程
     *
     * @param searchName
     * @return
     */
    @GetMapping("/search/{searchName}")
    public R<List<Course>> searchCourse(@PathVariable String searchName) {
        List<Course> course = courseService.searchCourse(searchName);
        return R.success(course);

    }

    /**
     * 列表查询课程目录
     *
     * @return
     */
    @GetMapping("/listCourse")
    public R<List<Course>> listCourse() {
        List<Course> courses = courseService.list();
        if (courses != null) {
            return R.success(courses);
        } else {
            return R.error(StatusEnum.SERVICE_ERROR);
        }
    }
    /**
     * 专业课程名称列表
     */
    @GetMapping("/listProfessional")
    public R<List<Professional>> listProfessional() {
        List<Professional> professionalList = professionalService.list();
        if (professionalList != null) {
            return R.success(professionalList);
        } else {
            return R.error(StatusEnum.SERVICE_ERROR);
        }
    }
    /**
     * 专业方向名称列表
     * @return
     */
    @GetMapping("listDirection")
    public R<List<Direction>> listDirection() {
        List<Direction> directions = directionService.list();
        if (directions != null) {
            return R.success(directions);
        } else {
            return R.error(StatusEnum.SERVICE_ERROR);
        }
    }

}
